package com.huaxinkai.mobile.controller;

import java.io.File;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;

/**
 * 文件上传下载controller
 * @author liuzh
 * @date 2017年1月7日下午11:31:28
 */
@SuppressWarnings("serial")
@Controller
@RequestMapping(value="/file")
public class FileController extends MBaseController{

	/**
	 * 图片上传
	 * @param file
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="/imgUpload.htm")
	public void imgUpload(
			@RequestParam("file")MultipartFile file,
			HttpServletRequest request,
			HttpServletResponse response){
		
		Writer out = null;
		
		Map<String, Object> resut = new HashMap<String, Object>();
		
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			
			out = response.getWriter();
			
			if(!file.isEmpty()){
				
				String path = request.getSession().getServletContext().getRealPath("/dynamic/upload");
				
				File dir = new File(path);
				
				if(!dir.isDirectory()){
					
					System.out.println("创建文件夹:"+path);
					
					dir.mkdirs();
					
				}else{
					System.out.println("文件夹:" + path + "已存在！");
				}
				
				File savePath = new File(path +"/"+ file.getOriginalFilename() );
				
				
				FileUtils.copyInputStreamToFile(file.getInputStream(), savePath);
				
				resut.put("status", true);
				resut.put("msg", "上传成功");
				
				resut.put("src", "/dynamic/upload/" + file.getOriginalFilename());
				
			}else{
				resut.put("status", false);
				resut.put("msg", "上传失败");
			}
			
			out.write( new Gson().toJson(resut) );
			out.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
	}
	/**
	 * 删除图片
	 * @param file
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="/deleteImg.htm")
	public void deleteImg(
			@RequestParam("name")String name,
			HttpServletRequest request,
			HttpServletResponse response){
		
		Writer out = null;
		
		Map<String, Object> resut = new HashMap<String, Object>();
		
		String path = request.getSession().getServletContext().getRealPath("/dynamic/upload/");
		
		File file = new File(path+name);
		
		System.out.println("删除文件："+file.getPath());
		
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			
			out = response.getWriter();
			
			if(file.isFile()){
				
				file.delete();
				
				resut.put("status", true);
				resut.put("msg", "删除成功");
				resut.put("name", name);
				
			}else{
				resut.put("status", false);
				resut.put("msg", "删除失败：可能路径不对找不到该图片");
			}
			
			out.write(new Gson().toJson(resut));
			out.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
	}
}
